# 定义一个函数来读取数据
from datetime import datetime


def read_data(file_name):
    # 初始化一个空数组来存储字典
    data_array = []

    # 打开文件并读取内容
    with open(file_name, 'r', encoding='GBK') as file:
        content = file.read()

        # 将"I"替换为"1"
        data = content.replace("I", "0")

        # 按"试品型号："分割数据
        items = data.split("试品型号：")[1:]  # 取出第一项后面的数据

        result = []

        for item in items:
            lines = item.strip().splitlines()
            test_data = {}

            for line in lines:
                if "测量电流" in line:
                    test_data['current'] = float(line.split("：")[-1].strip().split()[0])
                elif "测试电阻" in line:
                    test_data['resistor'] = float(line.split("：")[-1].strip().split()[0])
                elif "时间" in line:
                    old_time_str = line.split("：")[-1].strip().split()[1]
                    input_format = "%Y年%m月%d日%H时%M分"

                    # 将字符串转换为 datetime 对象
                    datetime_obj = datetime.strptime(old_time_str, input_format)
                    output_format = "%Y-%m-%d %H:%M:%S"
                    # 定义输出时间的格式
                    test_data['test_time'] = datetime_obj.strftime(output_format)
            result.append(test_data)

        return result


# 使用函数读取数据
data_array = read_data('C:\\Users\\Administrator\\Desktop\\data\\导通数据（接地导通仪，保定恒达）导出数据.txt')
# print(data_array)
# # 打印结果
for index, data in enumerate(data_array):
    print(f"数据组 {index + 1}: {data}")
